<?php

namespace App\Http\Controllers\AppApi\Member;

use App\admin\Service\OA;
use App\Models\Admin\ComOptionModel;
use App\Models\Admin\CompanyModel;
use App\Models\AreaModel;
use App\Models\User\UsergongchangModel;
use App\Models\User\UserModel;
use App\Models\User\UserOptionsGongchangModel;
use App\Models\User\UserOptionsGongshiModel;
use App\Models\UserQuick;
use App\Services\Admin\CompanyService;
use App\Utilities\Upload;
use Illuminate\Http\Request;

class FactoryController extends BaseController
{
    /**
     * 工厂接口控制器
     * 制作者：九哥
     */

    protected $service;

    public function __construct()
    {
        parent::__construct();
        $this->service = new CompanyService();
    }

    /**
     * ---------------------------------------
     * 工厂信息
     * ---------------------------------------
     */

    /**
     * 工厂类型列表
     */
    public function getFacTypeList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $facTypes = $this->service->getFacTypes();
        if (!$facTypes) {
            return $this->toJson(-3);
        }
        return $this->toJson(0, $facTypes);
    }

    /**
     * 工厂企业信息，获取
     */
    public function getComShow(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;

        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $model=UsergongchangModel::getModel()->getShowOneByUid($uid);
       // $model = $this->service->getOneByUid($uid, CompanyModel::type_gongchang);
        //  print_r($model->toArray());exit;
        if (!$model) {
            return $this->toJson(-3);
        }
        return $this->toJson(0, $model->toArray());
    }

    /**
     * 企业信息，上传
     * 待删除
     */
    public function setUpload(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $cate = $request->cate;
        /* if (!$token) {
             return redirect('/h5/error.html?errMsg=参数错误');
         }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return redirect('/h5/error.html?errMsg=用户未登录或未注册');
        }
        if (!isset($request->fileSfzzm) && !isset($request->fileSfzfm)
            && !isset($request->fileSfzsc)) {
            return redirect('/h5/error.html?errMsg=文件没有上传');
        }
        $facType = intval($request->facType);
        // $facType = $request->facType;
        $viewJump = $request->viewJump;
        $realname = $request->realname ? $request->realname : '';
        $sex = $request->sex;
        $mobile = $request->mobile ? $request->mobile : '';
        $idcard = $request->idcard ? $request->idcard : '';
        $sfzzm = $request->sfzzm ? $request->sfzzm : '';
        $sfzfm = $request->sfzfm ? $request->sfzfm : '';
        $sfzsc = $request->sfzsc ? $request->sfzsc : '';
        $images = array();
        $model = $this->service->getOneByUid($uid);
        if (isset($request->fileSfzzm)) {
            $resFile = Upload::uploadImgByApi($request, 'fileSfzzm');
            if ($resFile['code'] != 0) {
                return redirect('/h5/error.html?errMsg=' . $resFile['msg']);
            }
            $images = array(
                'sfzzm' => $resFile['filePath'],
                'sfzfm' => $sfzfm,
                'sfzsc' => $sfzsc,
            );
            if ($model && $model->legal_idcard_imgs) {
                $imageOlds = explode('|', $model->legal_idcard_imgs);
                if ($resFile['code'] == 0 && $imageOlds[0] &&
                    file_exists(ltrim($imageOlds[0], '/'))) {
                    unlink(ltrim($imageOlds[0], '/'));
                }
            }
        } else if (isset($request->fileSfzfm)) {
            $resFile = Upload::uploadImgByApi($request, 'fileSfzfm');
            if ($resFile['code'] != 0) {
                return redirect('/h5/error.html?errMsg=' . $resFile['msg']);
            }
            $images = array(
                'sfzzm' => $sfzzm,
                'sfzfm' => $resFile['filePath'],
                'sfzsc' => $sfzsc,
            );
            if ($model && $model->legal_idcard_imgs) {
                $imageOlds = explode('|', $model->legal_idcard_imgs);
                if ($resFile['code'] == 0 && $imageOlds[1] &&
                    file_exists(ltrim($imageOlds[1], '/'))) {
                    unlink(ltrim($imageOlds[1], '/'));
                }
            }
        } else if (isset($request->fileSfzsc)) {
            $resFile = Upload::uploadImgByApi($request, 'fileSfzsc');
            if ($resFile['code'] != 0) {
                return redirect('/h5/error.html?errMsg=' . $resFile['msg']);
            }
            $images = array(
                'sfzzm' => $sfzzm,
                'sfzfm' => $sfzfm,
                'sfzsc' => $resFile['filePath'],
            );
            if ($model && $model->legal_idcard_imgs) {
                $imageOlds = explode('|', $model->legal_idcard_imgs);
                if ($resFile['code'] == 0 && $imageOlds[2] &&
                    file_exists(ltrim($imageOlds[2], '/'))) {
                    unlink(ltrim($imageOlds[2], '/'));
                }
            }
        }
        $dataArr = array(
            'uid' => $uid,
            'cate' => $cate,
            'facType' => $facType,
            'legal_name' => $realname,
            'legal_sex' => $sex,
            'legal_mobile' => $mobile,
            'legal_idcard' => $idcard,
            'legal_idcard_imgs' => implode('|', $images),
            //下面给个初始值
            'com_name' => $model ? $model->com_name : '',
            'com_tel' => $model ? $model->com_tel : '',
            'com_fax' => $model ? $model->com_fax : '',
            'com_address' => $model ? $model->com_address : '',
            'age_range' => $model ? $model->age_range : '',
            'detail' => $model ? $model->detail : '',
            'lat' => $model ? $model->lat : '',
            'lng' => $model ? $model->lng : '',
        );
        $this->service->add($uid, $dataArr);
        return redirect($viewJump);
    }

    /**
     * 企业信息，业务类型
     */
    public function getOutTypeList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $outTypes = $this->service->getOutTypes($cate);
        return $this->toJson(0, $outTypes);
    }

    /**
     * 企业信息，设置
     */
    public function setModify(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $cate = $request->cate;
        if (!$token || !in_array($cate, [1, 2, 3])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $comInfo = $request->comInfo;
        $facType = $comInfo['facType'] ?? 0;
        $realname = $comInfo['realname'];
        $sex = $comInfo['sex'];
        $mobile = $comInfo['mobile'];
        $idcard = $comInfo['idcard'];
        $sfzzm = $comInfo['sfzzm'];
        $sfzfm = $comInfo['sfzfm'];
        $sfzsc = $comInfo['sfzsc'];
        $images = array(
            'sfzzm' => $sfzzm,
            'sfzfm' => $sfzfm,
            'sfzsc' => $sfzsc,
        );
        $comName = $comInfo['comName'];
        $comTel = $comInfo['comTel'];
        $comFax = $comInfo['comFax'];
        $comAddress = $comInfo['comAddress'];
        $comCity = $comInfo['comCity'];
        $position = $comInfo['position'];
        $cusLike = $comInfo['cusLike'];
        $quality = $comInfo['quality'];
        $pact = $comInfo['pact'];
        $param = $comInfo['param'];
        $param2 = $comInfo['param2'];
        $outTypeStr = $comInfo['outTypeStr'];
        $ageRange = $comInfo['ageRange'];
        $detail = $comInfo['detail'];
        //  $cityId = $this->service->getCityIdByName($comCity);
        // if (!$cityId) { return $this->toJson(-25); }

        if ($cusLike > 100) {
            OA::Json(1, '客户满意度填写错误');
        }
        if ($quality > 100) {
            OA::Json(1, '品质合格率填写错误');
        }
        if ($pact > 100) {
            OA::Json(1, '合同履行率填写错误');
        }

        //  $cityId=AreaModel::getAreaIdByName($comCity);
        $dataArr = array(
            'facType' => $facType,
            'cate' => $cate,
            'legal_name' => $realname,
            'legal_sex' => $sex,
            'legal_mobile' => $mobile,
            'legal_idcard' => $idcard,
            'legal_idcard_imgs' => implode('|', $images),
            'com_name' => $comName,
            'com_tel' => $comTel,
            'com_fax' => $comFax,
            'com_address' => $comAddress,
            'comCity' => $comCity,
            'position' => $position,
            'cus_like' => $cusLike,
            'quality' => $quality,
            'pact' => $pact,
            'param' => $param ? json_encode($param) : '',
            'param2' => $param2 ? json_encode($param2) : '',
            'out_type' => $outTypeStr,
            'age_range' => $ageRange,
            'detail' => $detail,
            'uid' => $uid,
            'shenhe' => 0
        );
        try {
            UsergongchangModel::getModel()->setModifyByUid($uid, $dataArr);
        //    $this->service->add($uid, $dataArr, CompanyModel::type_gongchang);
           UserQuick::removeFactoryQualified($uid);
        } catch (\Illuminate\Database\QueryException $e) {
            return $this->toJson(1, [], $e->getMessage());
            //OA::Json(1,'参数错误');
        }
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 工厂信息
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 企业认证
     * ---------------------------------------
     */

    /**
     * 企业认证，获取
     * api/member/company/filelist
     */
    public function getFileList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($cate, [1, 2, 3])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $model = $this->service->getOptionLicence($uid);
        if (!$model) {
            return $this->toJson(-3);
        }
        return $this->toJson(0, $model);
    }

    /**
     * 记录   企业信息
     */
    public function setFileUpload(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            OA::Json(1, '参数错误');
        }
        $token = $request->token;
        $uid = $this->accessGet($token);
        if (!$uid) {
            OA::Json(1, '没有登录');
        }
        $legal_licences = $request->post('legal_licences');
        $com_licences = $request->post('com_licences');
        $strType = $request->post('type');
        $type = CompanyModel::type_gongsi;
        if ($strType == 'gc') {
            $type = CompanyModel::type_gongchang;
        }


        if ($type == CompanyModel::type_gongsi) {
            $useroption = new UserOptionsGongshiModel();
            $sr = $useroption->setgongsikuozhanxinxi($uid, [
                'legal_licences' => $legal_licences,
                'com_licences' => $com_licences
            ]);
        }elseif($type == CompanyModel::type_gongchang){
               $sr =   UserOptionsGongchangModel::getModel()->setgognchangkuozhanxinxi($uid, [
                'legal_licences' => $legal_licences,
                'com_licences' => $com_licences
            ]);
        }else{
            OA::Json(1, '系统错误');
        }
        /*
        $sr = ComOptionModel::upDataJson([
            'legal_licences' => $legal_licences,
            'com_licences' => $com_licences
        ],$type);
         */
        if ($sr) {
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '系统错误');
        }
    }


    /**
     * 企业信息，上传
     * 待删除
     */
    private function setFileUpload_del(Request $request)
    {
        OA::Json(1, '弃用');
        if ($request->app_access != $this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $cate = $request->cate;
        $facType = $request->facType;
        $viewJump = $request->viewJump;
        if (!$token || !in_array($cate, [1, 2, 3])) {
            return redirect('/h5/error.html?errMsg=参数错误');
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return redirect('/h5/error.html?errMsg=用户未登录或未注册');
        }
        if (!isset($request->fileXxz) && !isset($request->fileSfz) &&
            !isset($request->fileByz) && !isset($request->fileJz) &&
            !isset($request->fileJhz) && !isset($request->fileJsz) &&
            !isset($request->fileZzjgdmz) && !isset($request->fileYyzz)
            && !isset($request->fileSwdjz) && !isset($request->fileZlht)
            && !isset($request->fileXkz) && !isset($request->fileRyzs)
            && !isset($request->fileFcz)) {
            return redirect('/h5/error.html?errMsg=文件没有上传');
        }
        if (isset($request->fileXxz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileXxz');
            $fileName = 'photos';
        } else if (isset($request->fileSfz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileSfz');
            $fileName = 'idcards';
        } else if (isset($request->fileByz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileByz');
            $fileName = 'graduates';
        } else if (isset($request->fileJz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileJz');
            $fileName = 'honors';
        } else if (isset($request->fileJhz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileJhz');
            $fileName = 'marrys';
        } else if (isset($request->fileJsz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileJsz');
            $fileName = 'drives';
        } else if (isset($request->fileZzjgdmz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileZzjgdmz');
            $fileName = 'zzjgdmzs';
        } else if (isset($request->fileYyzz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileYyzz');
            $fileName = 'yyzzs';
        } else if (isset($request->fileSwdjz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileSwdjz');
            $fileName = 'swdjzs';
        } else if (isset($request->fileZlht)) {
            $resFile = Upload::uploadImgByApi($request, 'fileZlht');
            $fileName = 'rents';
        } else if (isset($request->fileXkz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileXkz');
            $fileName = 'licencess';
        } else if (isset($request->fileRyzs)) {
            $resFile = Upload::uploadImgByApi($request, 'fileRyzs');
            $fileName = 'honorss';
        } else if (isset($request->fileFcz)) {
            $resFile = Upload::uploadImgByApi($request, 'fileFcz');
            $fileName = 'houses';
        } else {
            return redirect('/h5/error.html?errMsg=未知错误');
        }
        if ($resFile['code'] != 0) {
            return redirect('/h5/error.html?errMsg=' . $resFile['msg']);
        }
        $this->service->setOptionLicence($uid, $fileName, $resFile['filePath']);
        return redirect($viewJump);
    }

    /**
     * 企业认证，链接设置
     */
    public function setFileLink(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($cate, [1, 2, 3])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $photos = $request->photos;
        $idcards = $request->idcards;
        $graduates = $request->graduates;
        $honors = $request->honors;
        $marrys = $request->marrys;
        $drives = $request->drives;
        $zzjgdmzs = $request->zzjgdmzs;
        $yyzzs = $request->yyzzs;
        $swdjzs = $request->swdjzs;
        $rents = $request->rents;
        $licencess = $request->licencess;
        $honorss = $request->honorss;
        $houses = $request->houses;
        if (!$photos && !$idcards && !$graduates && !$honors &&
            !$marrys && !$drives && !$zzjgdmzs && !$yyzzs && !$swdjzs
            && !$rents && !$licencess && !$honorss && !$houses) {
            $this->toJson(-3);
        }
        $legals = array(
            'photos' => $photos ? $photos : '',
            'idcards' => $idcards ? $idcards : '',
            'graduates' => $graduates ? $graduates : '',
            'honors' => $honors ? $honors : '',
            'marrys' => $marrys ? $marrys : '',
            'drives' => $drives ? $drives : '',
        );
        $coms = array(
            'zzjgdmzs' => $zzjgdmzs ? $zzjgdmzs : '',
            'yyzzs' => $yyzzs ? $yyzzs : '',
            'swdjzs' => $swdjzs ? $swdjzs : '',
            'rents' => $rents ? $rents : '',
            'licencess' => $licencess ? $licencess : '',
            'honorss' => $honorss ? $honorss : '',
            'houses' => $houses ? $houses : '',
        );
        $this->service->setComFileLink($uid, $legals, $coms);
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 企业认证
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 产品
     * ---------------------------------------
     */

    /**
     * 产品，获取
     */
    public function getProductList(Request $request)
    {

        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        if (!$token || !in_array($cate, [1, 2, 3])) {
            return $this->toJson(-2);
        }
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $zhenList = $this->service->getProductList(19);
        $suoList = $this->service->getProductList(20);
        $baoList = $this->service->getProductList(21);
        $resPros =UserOptionsGongchangModel::getModel()->getOptionProList($uid);
       // $resPros = $this->service->getOptionPro($uid, CompanyModel::type_gongchang);
        $dataArr = array();
        $zhenIdStr = '';
        $suoIdStr = '';
        $baoIdStr = '';
        foreach ($zhenList as $k => $v) {
            $dataArr['zhens'][$k]['id'] = $v->id;
            $dataArr['zhens'][$k]['name'] = $v->name;
            if ($resPros && $zhens = $resPros['zhens']) {
                foreach ($zhens as $zhen) {
                    if ($zhen[0] == $v->id) {
                        $dataArr['zhens'][$k]['pros'] = $zhen;
                    }
                }
            } else {
                $dataArr['zhens'][$k]['pros'] = array(0, 0, 0, 0);
            }
            if ($zhenIdStr == '') {
                $zhenIdStr = $v->id;
            } else {
                $zhenIdStr .= '-' . $v->id;
            }
        }
        foreach ($suoList as $k => $v) {
            $dataArr['suos'][$k]['id'] = $v->id;
            $dataArr['suos'][$k]['name'] = $v->name;
            if ($resPros && $suos = $resPros['suos']) {
                foreach ($suos as $suo) {
                    if ($suo[0] == $v->id) {
                        $dataArr['suos'][$k]['pros'] = $suo;
                    }
                }
            } else {
                $dataArr['suos'][$k]['pros'] = array(0, 0, 0, 0);
            }
            if ($suoIdStr == '') {
                $suoIdStr = $v->id;
            } else {
                $suoIdStr .= '-' . $v->id;
            }
        }
        foreach ($baoList as $k => $v) {
            $dataArr['baos'][$k]['id'] = $v->id;
            $dataArr['baos'][$k]['name'] = $v->name;
            if ($resPros && $baos = $resPros['baos']) {
                foreach ($baos as $bao) {
                    if ($bao[0] == $v->id) {
                        $dataArr['baos'][$k]['pros'] = $bao;
                    }
                }
            } else {
                $dataArr['baos'][$k]['pros'] = array(0, 0, 0, 0);
            }
            if ($baoIdStr == '') {
                $baoIdStr = $v->id;
            } else {
                $baoIdStr .= '-' . $v->id;
            }
        }
        $dataArr['zhenIdStr'] = $zhenIdStr;
        $dataArr['suoIdStr'] = $suoIdStr;
        $dataArr['baoIdStr'] = $baoIdStr;
        return $this->toJson(0, $dataArr);
    }

    /**
     * 产品，设置
     */
    public function setFacProduct(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        /*   if (!$token) {
               return $this->toJson(-2);
           }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $strType = $request->post('type');


        $dataArr = array(
            'zhens' => json_decode($request->zhens, true),
            'suos' => json_decode($request->suos, true),
            'baos' => json_decode($request->baos, true)
        );
        UserOptionsGongchangModel::getModel()->setOptionProList($uid,$dataArr);
      //  $this->service->setOptionPro($uid, $dataArr, CompanyModel::type_gongchang);
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 产品
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 工厂设备
     * ---------------------------------------
     */

    /**
     * 工厂设备，获取
     */
    public function getFacEquipList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        $equipList = $this->service->getEquipList();
        if (!$equipList) {
            return $this->toJson(-3);
        }
        $resEquips=UserOptionsGongchangModel::getModel()->getOptionEquipList($uid);
      //  $resEquips = $this->service->getOptionEquip($uid, CompanyModel::type_gongchang);
//        $dataArr = array($equipList,$resEquips);
        $dataArr = array();
        $equipIdStr = '';
        foreach ($equipList as $k => $v) {
            $dataArr['equips'][$k]['id'] = $v->id;
            $dataArr['equips'][$k]['name'] = $v->name;
            $dataArr['equips'][$k]['brand'] = '';
            $dataArr['equips'][$k]['year'] = '';
            $dataArr['equips'][$k]['num'] = '';
            if ($resEquips) {
                foreach ($resEquips as $k2 => $resEquip) {
                    if ($resEquip[0] == $v->id) {
                        $dataArr['equips'][$k]['brand'] = $resEquip[1] ? $resEquip[1] : '';
                        $dataArr['equips'][$k]['year'] = $resEquip[2] ? $resEquip[2] : '';
                        $dataArr['equips'][$k]['num'] = $resEquip[3] ? $resEquip[3] : '';
                        $dataArr['equips'][$k]['check'] = $resEquip[4] ? $resEquip[4] : '';
                    }
                }
            }
            if ($equipIdStr == '') {
                $equipIdStr = $v->id;
            } else {
                $equipIdStr .= '-' . $v->id;
            }
        }
        $dataArr['equipIdStr'] = $equipIdStr;
        return $this->toJson(0, $dataArr);
    }

    /**
     * 工厂设备，设置
     */
    public function setFacEquip(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $equips = json_decode($request->equips, true);
        if (!$equips) {
            return $this->toJson(-3);
        }
        UserOptionsGongchangModel::getModel()->setOptionEquip($uid, $equips);
       // $this->service->setOptionEquip($uid, $equips, CompanyModel::type_gongchang);
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 工厂设备
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 工厂产量产能
     * ---------------------------------------
     */

    /**
     * 产量产能，获取
     */
    public function getFacYieldList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /*if (!$token) {
            return $this->toJson(-2);
        }*/
        $uid = $this->accessGet($token);
        $yields=UserOptionsGongchangModel::getModel()->getOptionYield($uid);
      //  $yields = $this->service->getOptionYield($uid, CompanyModel::type_gongchang);
        if (!$yields) {
            return $this->toJson(-3);
        }
        return $this->toJson(0, $yields);
    }

    /**
     * 产量产能，设置
     */
    public function setFacYield(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $yields = $request->yields;
        if (!$yields) {
            return $this->toJson(-3);
        }
        UserOptionsGongchangModel::getModel()->setOptionYield($uid, $yields);
       // $this->service->setOptionYield($uid, $yields, CompanyModel::type_gongchang);
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 工厂产量产能
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 工厂环境
     * ---------------------------------------
     */

    /**
     * 工厂环境，获取
     */
    public function getFacEnvFile(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        $strType = $request->post('type');
        $type = CompanyModel::type_gongsi;
        if ($strType == 'gc') {
            $type = CompanyModel::type_gongchang;
            $model = $this->service->getFacEnv($uid, $type);
        }
        if ($type == CompanyModel::type_gongsi) {
            $model = UserOptionsGongshiModel::getModel()->getFacEnvList($uid);
        }elseif ($type == CompanyModel::type_gongchang){
            $model = UserOptionsGongchangModel::getModel()->getFacEnvList($uid);
        }else{
             return $this->toJson(1,'错误');
        }
        //   var_dump($uid,$type);exit;
        //  $model = $this->service->getFacEnv($uid, $type);
        // print_r($model);exit;
        /*  if (!$model) {
              return $this->toJson(-3);
          }*/
        return $this->toJson(0, $model);
    }

    /**
     * 工厂环境，图片上传
     * 待删除
     */
    public function setFacEnvUpload(Request $request)
    {

        if ($request->app_access != $this->app_access) {
            OA::Json(1, '参数错误');
        }
        $token = $request->token;
        $uid = $this->accessGet($token);
        if (!$uid) {
            OA::Json(1, '没有登录');
        }
        $strType = $request->post('type');
        $type = CompanyModel::type_gongsi;
        if ($strType == 'gc') {
            $type = CompanyModel::type_gongchang;
        }


        $envs = $request->post('envs');

        if ($type == CompanyModel::type_gongsi) {
            $sr=UserOptionsGongshiModel::getModel()->upDataJson([
                'envs' => $envs,
            ]);
        } elseif($type == CompanyModel::type_gongchang) {
            $sr=UserOptionsGongchangModel::getModel()->upDataJson([
                'envs' => $envs,
            ]);
        }else{
             OA::Json(1, '系统错误');
        }


        if ($sr) {
            OA::Json(0, 'ok');
        } else {
            OA::Json(1, '系统错误');
        }


        /*
        if ($request->app_access!=$this->app_access) {
            return redirect('/h5/error.html?errMsg=接口权限错误');
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        $viewJump = $request->viewJump;
        if (!$token) {
            return redirect('/h5/error.html?errMsg=用户未登录或未注册');
        }
        $uid = $this->accessGet($token);
        if (!isset($request->managerFile) && !isset($request->facManagerFile) &&
            !isset($request->financeFile) && !isset($request->guardFile) &&
            !isset($request->shapeFile) && !isset($request->rawFile) &&
            !isset($request->techFile) && !isset($request->workFile) &&
            !isset($request->backFile) && !isset($request->cutFile) &&
            !isset($request->dormFile) && !isset($request->sampleFile)) {
            return redirect('/h5/error.html?errMsg=文件未上传');
        }
        if (isset($request->managerFile)) {
            $resFile = Upload::uploadImgByApi($request,'managerFile');
//            $this->service->setFacEnv($uid,'managers',$resFile['filePath']);
//            return redirect($viewJump);
            $fileName = 'managers';
        }
        else if (isset($request->facManagerFile)) {
            $resFile = Upload::uploadImgByApi($request,'facManagerFile');
            $fileName = 'facManagers';
        }
        else if (isset($request->financeFile)) {
            $resFile = Upload::uploadImgByApi($request,'financeFile');
            $fileName = 'finances';
        }
        else if (isset($request->guardFile)) {
            $resFile = Upload::uploadImgByApi($request,'guardFile');
            $fileName = 'guards';
        }
        else if (isset($request->shapeFile)) {
            $resFile = Upload::uploadImgByApi($request,'shapeFile');
            $fileName = 'shapes';
        }
        else if (isset($request->rawFile)) {
            $resFile = Upload::uploadImgByApi($request,'rawFile');
            $fileName = 'raws';
        }
        else if (isset($request->techFile)) {
            $resFile = Upload::uploadImgByApi($request,'techFile');
            $fileName = 'techs';
        }
        else if (isset($request->workFile)) {
            $resFile = Upload::uploadImgByApi($request,'workFile');
            $fileName = 'works';
        }
        else if (isset($request->backFile)) {
            $resFile = Upload::uploadImgByApi($request,'backFile');
            $fileName = 'backs';
        }
        else if (isset($request->cutFile)) {
            $resFile = Upload::uploadImgByApi($request,'cutFile');
            $fileName = 'cuts';
        }
        else if (isset($request->dormFile)) {
            $resFile = Upload::uploadImgByApi($request,'dormFile');
            $fileName = 'dorms';
        }
        else if (isset($request->sampleFile)) {
            $resFile = Upload::uploadImgByApi($request,'sampleFile');
            $fileName = 'samples';
        }
        else {
            return redirect('/h5/error.html?errMsg=未知错误');
        }
        if ($resFile['code']!=0) {
            return redirect('/h5/error.html?errMsg=图片错误');
        }
        $this->service->setFacEnv($uid,$fileName,$resFile['filePath']);
        return redirect($viewJump);

        */
    }

    /**
     * 工厂环境，链接设置
     */
    public function setFacEnvLink(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        $managers = $request->managers;
        $facManagers = $request->facManagers;
        $finances = $request->finances;
        $guards = $request->guards;
        $shapes = $request->shapes;
        $raws = $request->raws;
        $techs = $request->techs;
        $works = $request->works;
        $backs = $request->backs;
        $cuts = $request->cuts;
        $dorms = $request->dorms;
        $samples = $request->samples;
        if (!$managers && !$facManagers && !$finances && !$guards &&
            !$shapes && !$raws && !$techs && !$works && !$backs
            && !$cuts && !$dorms && !$samples) {
            $this->toJson(-3);
        }
        $dataArr = array(
            'managers' => $managers ? $managers : '',
            'facManagers' => $facManagers ? $facManagers : '',
            'finances' => $finances ? $finances : '',
            'guards' => $guards ? $guards : '',
            'shapes' => $shapes ? $shapes : '',
            'raws' => $raws ? $raws : '',
            'techs' => $techs ? $techs : '',
            'works' => $works ? $works : '',
            'backs' => $backs ? $backs : '',
            'cuts' => $cuts ? $cuts : '',
            'dorms' => $dorms ? $dorms : '',
            'samples' => $samples ? $samples : '',
        );
        $this->service->setComEnvLink($uid, $dataArr);
        return $this->toJson(0, $dataArr);
    }

    /**
     * ---------------------------------------
     * 工厂环境
     * ---------------------------------------
     */


    /**
     * ---------------------------------------
     * 工厂原料面料
     * ---------------------------------------
     */

    /**
     * 获取
     */
    public function getFacFabricList(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /*   if (!$token) {
               return $this->toJson(-2);
           }*/
        $uid = $this->accessGet($token);
        $models = $this->service->getCost($uid);
        if (!$models) {
            return $this->toJson(-3);
        }
        $dataArr = $models;
        return $this->toJson(0, $dataArr);
    }

    /**
     * 设置
     */
    public function setFacFabric(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /* if (!$token) {
             return $this->toJson(-2);
         }*/
        $uid = $this->accessGet($token);
        $costs = $request->costs;
        if (!$costs) {
            return $this->toJson(-3);
        }
        $this->service->setCost($uid, $costs);
        return $this->toJson(0);
    }

    /**
     * ---------------------------------------
     * 工厂原料面料
     * ---------------------------------------
     */

    /**
     * 资料的完善度
     */
    public function getFacInfoPer(Request $request)
    {
        if ($request->app_access != $this->app_access) {
            return $this->toJson(-1);
        }
        $token = $request->token;
        $type = $request->type;
        $cate = $request->cate;
        /*if (!$token) {
            return $this->toJson(-2);
        }*/
        $uid = $this->accessGet($token);
        if (!$uid) {
            return $this->toJson(-13);
        }
        $fac = $this->service->getOneByUid($uid);
        $facFile = $this->service->getOptionLicence($uid);
        $facPro = $this->service->getOptionPro($uid);
        $facEquip = $this->service->getOptionEquip($uid);
        $facYield = $this->service->getOptionYield($uid);
        $facFabric = $this->service->getCost($uid);
        $facEnv = $this->service->getFacEnv($uid);
        $dataArr = array( //百分比
            'factory' => 0,
            'facFile' => 0,
            'facPro' => 0,
            'facEquip' => 0,
            'facYield' => 0,
            'facFabric' => 0,
            'facEnv' => 0
        );
        //基本信息
        if ($fac) {
            $dataArr['factory'] = 100;
        }
        //认证
        if ($facFile) {
            $dataArr['facFile'] = 100;
        }
        //产品
        if ($facPro) {
            $dataArr['facPro'] = 100;
        }
        //设备
        if ($facEnv) {
            $dataArr['facEnv'] = 100;
        }
        //产量产能
        if ($facYield) {
            $dataArr['facYield'] = 100;
        }
        //面料辅料
        if ($facFabric) {
            $dataArr['facFabric'] = 100;
        }
        //环境
        if ($facEnv) {
            $dataArr['facEnv'] = 100;
        }
        return $this->toJson(0, $dataArr);
    }
}